<template>
	<view class="container">
		<view class="comment-list">
			<view class="comment-item" v-for="(item, i) in reviews" :key="i">
				<view class="left">
<!--                    <u-avatar size="60rpx" v-if="item?.user" :src="checkAndFixUrl(item?.user?.avatar)"></u-avatar>-->
					<u-avatar size="60rpx" :src="getServerStatic('/static/images/avatar.png')"></u-avatar>
				</view>
				<view class="right">
					<view class="nickname-box">
						<view class="nickname">{{ item?.user?.name ?? '用户' }}</view>
						<view class="date">{{ item.created_at }}</view>
					</view>
					<view class="stars">
						<u-rate inactiveIcon="star-fill" readonly inactiveColor="#DCDEE0" activeColor="#FBA808" gutter="6rpx" v-model="item.score"></u-rate>
					</view>

					<view class="comment-box">
                        {{ item.content }}
					</view>
					<view class="img-list"  v-if="item?.images">
						<view class="img-item" v-for="(value ,index) in item.images" :key="index">
                            <u-image radius="10rpx" width="110rpx" height="110rpx"
                                     :src="checkAndFixUrl(value)"></u-image>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>

    <u-loadmore :status="loadMoreStatus" />
</template>

<script setup>
    import {ref, reactive, readonly} from "vue"
    import {onLoad, onShow, onReachBottom} from '@dcloudio/uni-app'
    import {getAllReview} from "@/api/product";
    import {checkAndFixUrl, getServerStatic} from "@/utils/tool";
    const loadMoreStatus = ref('loadmore')

    const currentPage = ref(1)
    const perPage = ref(10)
    // 商品ID
    const productId = ref(0)

	const star = ref(1)
    // 评论
	const reviews = ref([])

    onLoad((evt) => {
        console.log('评论', evt)
        productId.value = evt?.product_id ?? 0
        handleGetCommons()
    })

    const getSearchParams = () => {
        return {
            product_id: productId.value ?? 0,
            currentPage: currentPage.value ?? 1,
            perPage: perPage.value ?? 10,
        }
    }

    const handleGetCommons = async () => {
        const param = getSearchParams()

        const { code, data } = await getAllReview(param)
        if (code == 1) {
            if (data.length <= 0) {
                loadMoreStatus.value = 'nomore'
                console.log('noting')
            } else {
                currentPage.value++
                // 数量少于每页数量
                if (data.length < perPage.value) {
                    loadMoreStatus.value = 'nomore'
                } else {
                    loadMoreStatus.value = 'loadmore'
                }
            }
            reviews.value = reviews.value.concat(data)
        } else {
            loadMoreStatus.value = 'nomore'
        }
        console.log(data)
    }

</script>

<style lang="scss" scoped>
	.container {
		padding: 52rpx 26rpx;
	}
	.comment-list {
		.comment-item {
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 36rpx 20rpx 1rpx;
			@include between(100%);
			align-items: flex-start;
			margin-bottom: 20rpx;
			.right {
				flex: 1;
				margin-left: 20rpx;
				.nickname-box {
					@include between(100%);
					.nickname {
						color: #333333;
						font-size: 24rpx;
					}
					.date {
						font-size: 22rpx;
						color: #CCCCCC;
					}
				}
				.stars {
					margin-top: 10rpx;
				}
				.comment-box {
					margin-top: 24rpx;
					font-size: 24rpx;
					line-height: 32rpx;
					color: #666666;
					margin-bottom: 28rpx;
				}
				.img-list {
					display: flex;
					align-items: center;
					flex-wrap: wrap;
					.img-item {
						margin-right: 21rpx;
						margin-bottom: 28rpx;
					}
				}
			}
		}
	}
</style>
